package net.zoneland.knowledge.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Preconditions;

import net.zoneland.knowledge.controller.bean.UomUserOrgQueryVo;
import net.zoneland.knowledge.model.ResponseResult;
import net.zoneland.knowledge.model.ResponseResultRest;
import net.zoneland.knowledge.service.PartyService;

/**
 * uom人员组织控制器.
 * @author zmq
 * @Date 2022/7/29 15:32
 */
@RestController
@RequestMapping("/ua")
public class UomUserOrgController extends AbstractBaseController {
    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(UomUserOrgController.class);

    /**
     * 老的员工编号长度.
     */
    private static final int OLD_YG_ID_LENGTH = 8;

    /**
     * 党员/党组织业务接口.
     */
    @Autowired
    private PartyService partyService;

    /**
     * 模糊查询人员信息.
     * @param request http请求
     * @param vo 查询参数对象
     * @return 用户集合响应结果
     */
    @PostMapping("/queryUsers")
    public ResponseResult queryUsers(HttpServletRequest request, @RequestBody UomUserOrgQueryVo vo) {
        ResponseResult res = new ResponseResult();
        try {
            Preconditions.checkNotNull(vo, "查询参数对象不能为空！");
            vo.setUserPojo(getAndCheckCurrentUser(request));
            vo.setQueryTextForMobile(vo.getUserName());
            vo.setQueryTextForYgId(doFillYgId(StringUtils.upperCase(vo.getUserName())));
            res.setData(partyService.searchByCondition(vo));
        } catch (Exception e) {
            LOGGER.error("模糊查询人员信息异常，vo:{}", vo, e);
            res.setResult(ResponseResult.FAILED_RESULT);
            res.setMessage("模糊查询人员信息异常：" + e.getMessage());
        }
        return res;
    }

    /**
     * 处理员工编号.
     * @param ygId 员工编号
     * @return 员工编号
     */
    private static String doFillYgId(final String ygId) {
        if(StringUtils.isNumeric(ygId) && StringUtils.length(ygId) == OLD_YG_ID_LENGTH) {
            return "E00" + ygId;
        }
        return ygId;
    }

    /**
     * 模糊查询部门信息.
     * @param request http请求
     * @param vo 查询参数对象
     * @return 用户集合响应结果
     */
    @PostMapping("/queryDepts")
    public ResponseResult queryDepts(HttpServletRequest request, @RequestBody UomUserOrgQueryVo vo) {
        ResponseResult res = new ResponseResult();
        try {
            Preconditions.checkNotNull(vo, "查询参数对象不能为空！");
            Preconditions.checkArgument(StringUtils.isNotBlank(vo.getOrgName()), "模糊搜索的部门名称不能为空！");
            res.setData(partyService.searchByOrgName(vo.getOrgName(), 1, vo.getSize(), getAndCheckCurrentUser(request)));
        } catch (Exception e) {
            LOGGER.error("模糊查询部门信息异常，vo:{}", vo, e);
            res.setResult(ResponseResult.FAILED_RESULT);
            res.setMessage("模糊查询部门员信息异常：" + e.getMessage());
        }
        return res;
    }

    /**
     * 根据时间查询党元信息.
     * @param start 开始时间
     * @return
     */
    @GetMapping("/getUserByTime")
    public ResponseResultRest getUserByTime(final String start) {
        ResponseResultRest res = ResponseResultRest.success();
        try {
            Preconditions.checkNotNull(start, "查询参数不能为空！");
            res.setData(partyService.getUserByTime(start));
        } catch (Exception e) {
            LOGGER.error("getUserByTime error ，beginTime: {}", start, e);
            res = ResponseResultRest.failure(e.getMessage());
        }
        return res;
    }

    /**
     * 根据时间查询党组织信息.
     * @param start 开始时间
     * @return
     */
    @GetMapping("/getOrgByTime")
    public ResponseResultRest getOrgByTime(final String start) {
        ResponseResultRest res = ResponseResultRest.success();
        try {
            Preconditions.checkNotNull(start, "查询参数不能为空！");
            res.setData(partyService.getOrgByTime(start));
        } catch (Exception e) {
            LOGGER.error("getOrgByTime error ，beginTime: {}", start, e);
            res = ResponseResultRest.failure(e.getMessage());
        }
        return res;
    }

}
